package com.app.server.impl;

import cn.hutool.core.util.DesensitizedUtil;
import com.app.common.ConstantTool;
import com.app.mapper.*;
import com.app.model.*;
import com.app.model.DisasterReportingModel;
import com.app.server.DisasterReportingService;
import com.app.server.RecordFileService;
import com.app.server.UserService;
import com.app.utils.StringTool;
import com.app.vo.*;
import com.app.vo.DisasterReportingVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 报灾信息
 */
@Service
public class DisasterReportingServiceImpl extends ServiceImpl<DisasterReportingMapper, DisasterReportingModel> implements DisasterReportingService {
    @Autowired
    private DisasterReportingMapper disasterReportingMapper;

    @Autowired
    private DisasterHandleMapper disasterHandleMapper;

    @Autowired
    private RecordFileMapper recordFileMapper;

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private AdminUserMapper adminUserMapper;

    @Autowired
    private UserService userService;

    @Autowired
    private UnitsManagerMapper unitsManagerMapper;

    @Autowired
    private UnitsDisasterMapper unitsDisasterMapper;

    @Autowired
    private RecordFileService recordFileService;

    //列表
    @Override
    public DisasterReportingVo getDisasterList(Integer page, Integer limit, Integer departmentId, Integer type,Integer claimsSettlementStatus) {
        Page<DisasterReportingModel> page1 = new Page<>(page, limit);
        QueryWrapper<DisasterReportingModel> qwType = new QueryWrapper<>();
        if (type != null) {
            qwType.eq("type", type);
        }
        if (claimsSettlementStatus != null) {
            qwType.eq("claims_settlement_status", claimsSettlementStatus).eq("allocation_status",3);
        }
        if (departmentId != null) {
            UnitsManagerModel unitsManager = unitsManagerMapper.selectById(departmentId);
            String name = unitsManager.getName();
            qwType.eq("real_name", name);
        }
        qwType.orderByDesc("create_time");
        Page<DisasterReportingModel> disasterPage = disasterReportingMapper.selectPage(page1, qwType);
        List<DisasterReportingModel> disasterList = disasterPage.getRecords();
        for (DisasterReportingModel disasterReporting : disasterList) {
            //报灾信息管理设置用户姓名、用户手机号
            String openId = disasterReporting.getOpenId();
            QueryWrapper<UserModel> qwUser = new QueryWrapper<>();
            qwUser.eq("open_id", openId);
            UserModel user = userService.getOne(qwUser);
            if (disasterReporting.getIsAnonymous() == 1) {
                disasterReporting.setUsername(DesensitizedUtil.chineseName(user.getName()));
                disasterReporting.setPhone(DesensitizedUtil.mobilePhone(user.getPhone()));
            } else {
                disasterReporting.setUsername(user.getName());
                disasterReporting.setPhone(user.getPhone());
            }
            //报灾信息管理设置灾害类型
            String disasterType = disasterReporting.getType();
            QueryWrapper<CategoryModel> qw = new QueryWrapper<>();
            qw.eq("id", disasterType);
            CategoryModel category = categoryMapper.selectOne(qw);
            disasterReporting.setTypeText(category.getName());
            Integer allocationStatus = disasterReporting.getAllocationStatus();
            if (allocationStatus == 1) {
                disasterReporting.setStatusText("待分配");
            }
            if (allocationStatus == 2) {
                disasterReporting.setStatusText("已分配");
            }
            if (allocationStatus == 3) {
                disasterReporting.setStatusText("已办结");
            }
            disasterReporting.setOpenId(null);
        }
        long count = disasterPage.getTotal();
        DisasterReportingVo disasterReportingVo = new DisasterReportingVo();
        disasterReportingVo.setList(disasterList);
        disasterReportingVo.setCount((int) count);
        return disasterReportingVo;
    }

    //根据单位ID查询单位列表
    @Override
    public DisasterReportingVo getDisasterByUnitsList(Integer page, Integer limit, Integer departmentId, Integer type, Integer unitsId,Integer claimsSettlementStatus) {
        Page<DisasterReportingModel> page1 = new Page<>(page, limit);
        QueryWrapper<DisasterReportingModel> qwDisaster = new QueryWrapper<>();

        QueryWrapper<UnitsDisasterModel> qwUnits = new QueryWrapper<>();
        qwUnits.eq("units_id", unitsId);
        List<UnitsDisasterModel> unitsDisasterList = unitsDisasterMapper.selectList(qwUnits);

        List<Integer> disasterIdList = new ArrayList<>();
        for (UnitsDisasterModel unitsDisaster : unitsDisasterList) {
            Integer disasterId = unitsDisaster.getDisasterId();
            disasterIdList.add(disasterId);
        }
//        if (disasterIdList.size() != 0) {
//            qwDisaster.in("id", disasterIdList);
//        }
        if (disasterIdList.isEmpty()) {
            DisasterReportingVo disasterReportingVo = new DisasterReportingVo();
            return disasterReportingVo;
        }
        if (!disasterIdList.isEmpty()) {
            qwDisaster.in("id", disasterIdList);
        }

        if (claimsSettlementStatus != null) {
            qwDisaster.eq("claims_settlement_status", claimsSettlementStatus).eq("allocation_status",3);
        }
        if (type != null) {
            qwDisaster.eq("type", type);
        }
        if (departmentId != null) {
            UnitsManagerModel unitsManager = unitsManagerMapper.selectById(departmentId);
            String name = unitsManager.getName();
            qwDisaster.eq("real_name", name);
        }
        qwDisaster.orderByDesc("id");
        Page<DisasterReportingModel> disasterPage = disasterReportingMapper.selectPage(page1, qwDisaster);
        List<DisasterReportingModel> disasterList = disasterPage.getRecords();
        for (DisasterReportingModel disasterReporting : disasterList) {
            //报灾信息管理设置用户姓名、用户手机号
            String openId = disasterReporting.getOpenId();
            QueryWrapper<UserModel> qwUser = new QueryWrapper<>();
            qwUser.eq("open_id", openId);
            UserModel user = userService.getOne(qwUser);
            disasterReporting.setUsername(user.getName());
            disasterReporting.setPhone(user.getPhone());
            //报灾信息管理设置灾害类型
            String disasterType = disasterReporting.getType();
            QueryWrapper<CategoryModel> qw = new QueryWrapper<>();
            qw.eq("id", disasterType);
            CategoryModel category = categoryMapper.selectOne(qw);
            disasterReporting.setTypeText(category.getName());
            Integer handleStatus = disasterReporting.getHandleStatus();
            if (handleStatus == 1) {
                disasterReporting.setStatusText("待处理");
            }
            if (handleStatus == 2) {
                disasterReporting.setStatusText("已处理");
            }
            disasterReporting.setOpenId(null);
        }
        long count = disasterPage.getTotal();
        DisasterReportingVo disasterReportingVo = new DisasterReportingVo();
        disasterReportingVo.setList(disasterList);
        disasterReportingVo.setCount((int) count);
        return disasterReportingVo;

    }


    @Override
    public DisasterReportingVo getUserDisasterList(Integer page, Integer limit, Integer type, String openId) {
        Page<DisasterReportingModel> page1 = new Page<>(page, limit);
        QueryWrapper<DisasterReportingModel> qwType = new QueryWrapper<>();
        if (type != null) {
            qwType.eq("type", type);
        }

        qwType.eq("open_id", openId);
        Page<DisasterReportingModel> disasterPage = disasterReportingMapper.selectPage(page1, qwType);
        List<DisasterReportingModel> disasterList = disasterPage.getRecords();

        QueryWrapper<UserModel> qwUser = new QueryWrapper<>();
        qwUser.eq("open_id", openId);
        UserModel user = userService.getOne(qwUser);
        for (DisasterReportingModel disasterReporting : disasterList) {
            //报灾信息管理设置用户姓名、用户手机号
            disasterReporting.setUsername(user.getName());
            disasterReporting.setPhone(user.getPhone());
            //报灾信息管理设置灾害类型
            String disasterType = disasterReporting.getType();
            QueryWrapper<CategoryModel> qw = new QueryWrapper<>();
            qw.eq("id", disasterType);
            CategoryModel category = categoryMapper.selectOne(qw);
            disasterReporting.setTypeText(category.getName());
            disasterReporting.setOpenId(null);
        }
        long count = disasterPage.getTotal();
        DisasterReportingVo disasterReportingVo = new DisasterReportingVo();
        disasterReportingVo.setList(disasterList);
        disasterReportingVo.setCount((int) count);
        return disasterReportingVo;
    }

    //野生动物救助根据类型查
    @Override
    public DisasterReportingVo getDisasterByType(Integer page, Integer limit, Integer type) {
        Page<DisasterReportingModel> page1 = new Page<>(page, limit);
        QueryWrapper<DisasterReportingModel> qwType = new QueryWrapper<>();
        qwType.eq("type", type);

        Page<DisasterReportingModel> disasterPage = disasterReportingMapper.selectPage(page1, qwType);
        List<DisasterReportingModel> disasterList = disasterPage.getRecords();
        for (DisasterReportingModel disasterReporting : disasterList) {
            String disasterType = disasterReporting.getType();
            QueryWrapper<CategoryModel> qw = new QueryWrapper<>();
            qw.eq("id", disasterType);
            CategoryModel category = categoryMapper.selectOne(qw);
            disasterReporting.setTypeText(category.getName());
            disasterReporting.setOpenId(null);
        }
        long count = disasterPage.getTotal();
        DisasterReportingVo disasterReportingVo = new DisasterReportingVo();
        disasterReportingVo.setList(disasterList);
        disasterReportingVo.setCount((int) count);
        return disasterReportingVo;
    }

    //获取灾害各类型统计饼图
    @Override
    public List<ValueAndNameVo> getDisasterTypeCount() {
        List<ValueAndNameVo> disasterTypeCount = disasterReportingMapper.getDisasterTypeCount();
        return disasterTypeCount;
    }

    //获取每个月各报灾类型统计折线图
    @Override
    public DisasterTypeVo getDisasterMonthCount() {
        String[] arrMonth = ConstantTool.arrMonth;
        String year = "%" + "2023" + "-";
        List<Integer> diZhen = new ArrayList<>();
        for (int i = 1; i <= 12; i++) {
            String month = year + arrMonth[i] + "%";
            Integer typeMonthCount = disasterReportingMapper.getTypeMonthCount(2, month);
            diZhen.add(typeMonthCount);
        }

        List<Integer> hongShui = new ArrayList<>();
        for (int i = 1; i <= 12; i++) {
            String month = year + arrMonth[i] + "%";
            Integer typeMonthCount = disasterReportingMapper.getTypeMonthCount(3, month);
            hongShui.add(typeMonthCount);
        }
        List<Integer> taiFeng = new ArrayList<>();
        for (int i = 1; i <= 12; i++) {
            String month = year + arrMonth[i] + "%";
            Integer typeMonthCount = disasterReportingMapper.getTypeMonthCount(4, month);
            taiFeng.add(typeMonthCount);
        }
        List<Integer> qiTa = new ArrayList<>();
        for (int i = 1; i <= 12; i++) {
            String month = year + arrMonth[i] + "%";
            Integer typeMonthCount = disasterReportingMapper.getTypeMonthCount(2, month);
            qiTa.add(typeMonthCount);
        }

        DisasterTypeVo disasterTypeVo = new DisasterTypeVo();

        List<String> monthList = new ArrayList<>();
        for (int i = 1; i <= 12; i++) {
            String month = String.valueOf(i);
            month = month + "月";
            monthList.add(month);
        }

        disasterTypeVo.setXData(monthList);
        disasterTypeVo.setDiZhen(diZhen);
        disasterTypeVo.setHongShui(hongShui);
        disasterTypeVo.setTaiFeng(taiFeng);
        disasterTypeVo.setQiTa(qiTa);
        return disasterTypeVo;
    }

    //获取每个月各报灾类型统计折线图
    @Override
    public DisasterTypeVo2 getDisasterMonthCount2() {
        String[] arrMonth = ConstantTool.arrMonth;
        String year = "%" + "2023" + "-";
        List<IdNameCountVo> idNameCountList = new ArrayList<>();
        QueryWrapper<CategoryModel> qw = new QueryWrapper<>();
        qw.eq("pid", 1);
        List<CategoryModel> categoryList = categoryMapper.selectList(qw);
        for (CategoryModel category : categoryList) {
            IdNameCountVo idName = new IdNameCountVo();
            Integer typeId = category.getId();
            idName.setId(typeId);
            idName.setName(category.getName());
            idNameCountList.add(idName);
            List<Integer> countList = new ArrayList<>();
            for (int i = 1; i <= 12; i++) {
                String month = year + arrMonth[i] + "%";
                Integer typeMonthCount = disasterReportingMapper.getTypeMonthCount(typeId, month);
                countList.add(typeMonthCount);
            }
            idName.setCount(countList);
        }
        DisasterTypeVo2 disasterTypeVo = new DisasterTypeVo2();
        disasterTypeVo.setYData(idNameCountList);

        List<String> monthList = new ArrayList<>();
        for (int i = 1; i <= 12; i++) {
            String month = String.valueOf(i);
            month = month + "月";
            monthList.add(month);
        }
        disasterTypeVo.setXData(monthList);


        return disasterTypeVo;
    }

    @Override
    public Integer selectCountByType(Integer id) {
        QueryWrapper<DisasterReportingModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", id);
        return disasterReportingMapper.selectCount(queryWrapper);
    }

    //处理状态为3时 带处理对象
    @Override
    public DisasterReportingModel setDisasterHandle(DisasterReportingModel disasterReporting) {
        QueryWrapper<DisasterHandleModel> qwHandle = new QueryWrapper<>();
        qwHandle.eq("disaster_id", disasterReporting.getId());
        DisasterHandleModel disasterHand = disasterHandleMapper.selectOne(qwHandle);
        List<String> urlList = new ArrayList<>();
        List<String> videoList = new ArrayList<>();
        String fileIdArr = disasterHand.getFileId();
        if (StringTool.isNotEmpty(fileIdArr)) {
            String[] split = fileIdArr.split(",");
            for (String fileIds : split) {
                int fileId = Integer.parseInt(fileIds);
                RecordFileModel recordFile = recordFileMapper.selectById(fileId);
                if (recordFile != null) {
                    if (recordFile.getFileType() != null && recordFile.getFileType() == 1) {
                        urlList.add(recordFile.getUrl());
                    }
                    if (recordFile.getFileType() != null && recordFile.getFileType() == 2) {
                        videoList.add(recordFile.getUrl());
                    }
                }
            }
            disasterHand.setUrlList(urlList);
            disasterHand.setVideoList(videoList);
        }


        disasterReporting.setDisasterHandle(disasterHand);
        return disasterReporting;
    }

    @Override
    public List<UserModel> whiteList() {
        return disasterReportingMapper.whiteList();
    }
}
